﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using MyBasicLib.Collections.Generic;
using MyBasicLib.Reflection;

namespace MyBasicLib.Extensions
{
    public static class EnumerableExtension
    {
        public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
        {
            foreach (T e in enumerable)
            {
                action(e);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable"></param>
        /// <param name="index">start from 1</param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList<T>(this IEnumerable<T> enumerable, int index, int pageSize)
        {
            return new PagedList<T>(enumerable.Skip((index - 1)*pageSize).Take(pageSize).ToList(), index, pageSize,
                                    enumerable.Count());
        }
         

        /// <summary>
        /// 转换为一个DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable"></param>
        /// <returns></returns>
        public static DataTable ToDataTable<T>(this IEnumerable<T> enumerable) where T : class
        {
            T t = default(T);
            //创建属性的集合
          var pList = TypeDataCache.GetTypeData(t).GetPropertyInfos();
            //获得反射的入口
            Type type = typeof (T);
            var dt = new DataTable(type.Name);

            //把所有的public属性加入到集合 并添加DataTable的列
            pList.ForEach(p =>
                              {
                                  dt.Columns.Add(p.Key, p.Value.PropertyType.IsGenericType &&
                                                          p.Value.PropertyType.GetGenericTypeDefinition() ==
                                                         typeof (Nullable<>)
                                                             ? p.Value.PropertyType.GetGenericArguments()[0]
                                                             : p.Value.PropertyType);
                              });
            foreach (T item in enumerable)
            {
                //创建一个DataRow实例
                DataRow row = dt.NewRow();
                //给row 赋值
                pList.ForEach(p => row[p.Key] = p.Value.FastGetValue(item) ?? DBNull.Value);
                //加入到DataTable
                dt.Rows.Add(row);
            }
            return dt;
        }

        public static List<T> ToList<T>(this DataTable dt) where T : class,new()
        {
            //创建一个属性的列表
            var pList = TypeDataCache.GetTypeData<T>().GetPropertyInfos();

            //获取TResult的类型实例  反射的入口

            //创建返回的集合
            List<T> oblist = new List<T>();

            foreach (DataRow row in dt.Rows)
            {
                //创建TResult的实例
                T ob = new T();
                //找到对应的数据  并赋值
                pList.ForEach(p => { if (row[p.Key] != DBNull.Value) p.Value.FastSetValue(ob, row[p.Key]); });
                //放入到返回的集合中.
                oblist.Add(ob);
            }
            return oblist;

        }
    }
}